package com.tengju.bff.interfaces.app.user.facade;

import com.tengju.bff.interfaces.manage.star.vo.StarExamResultVO;
import com.tengju.user.domain.model.sign.ExamQuestionAnswer;
import com.tengju.user.domain.model.sign.StarExamResultEnum;
import com.tengju.user.domain.model.sign.StarSignException;
import com.tengju.user.domain.model.survey.*;
import com.tengju.user.domain.shared.JsonUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StarSignFacade {

    @Value("${star.operator.exam.answer}")
    private String operatorExamAnswerConf;

    @Value("${star.service.provider.exam.answer}")
    private String serviceProviderExamAnswerConf;

    @Value("${star.service.provider.exam.id}")
    Long serviceProviderExamId;

    @Value("${star.operator.exam.id}")
    Long operatorExamId;

    @Autowired
    SurveyRepotiory surveyRepotiory;

    public StarExamResultVO submitStarExam(SurveyContent surveyContent) {
        List<ExamQuestionAnswer> examQuestionAnswers = new ArrayList<>();
        Long surveyId = surveyContent.getSurveyId().getValue();
        SurveyMeta surveyMeta = surveyRepotiory.getMetaById(surveyId);
        if (surveyMeta == null) {
            throw new StarSignException("问卷id不存在，" + surveyId);
        }
        if (surveyId.equals(operatorExamId)) {
            examQuestionAnswers = JsonUtil.toList(operatorExamAnswerConf, new TypeReference<List<ExamQuestionAnswer>>() {});
        } else {
            examQuestionAnswers = JsonUtil.toList(serviceProviderExamAnswerConf, new TypeReference<List<ExamQuestionAnswer>>() {});
        }
        StarExamResultVO starExamResultVO = new StarExamResultVO();
        List<SurveyQuestionResult> questionResults = surveyContent.getQuestionResults();
        Map<String, ExamQuestionAnswer> questionAnswerMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(questionResults)) {
            questionAnswerMap = examQuestionAnswers.stream()
                    .collect(Collectors.toMap(ExamQuestionAnswer::getQuestionCode, v -> v, (v1, v2) -> v1));
        }
        for (SurveyQuestionResult surveyQuestionResult : questionResults) {
            String questionCode = surveyQuestionResult.getQuestionCode();
            ExamQuestionAnswer examQuestionAnswer = questionAnswerMap.get(questionCode);
            if (examQuestionAnswer == null) {
                throw new StarSignException("问卷题目出错，" + questionCode);
            }
            boolean questionResult = examQuestionAnswer.judgeAnswer(surveyQuestionResult);
            if (!questionResult) {
                starExamResultVO.addErrorQuestion(surveyQuestionResult.getQuestionOrder() + "",surveyQuestionResult.getQuestionTitle());
            } else {
                starExamResultVO.addScore(examQuestionAnswer.getQuestionScore());
            }
        }
        if (surveyMeta.checkScore(starExamResultVO.getExamScore())) {
            starExamResultVO.setExamResult(StarExamResultEnum.PASS.getCode());
        } else {
            starExamResultVO.setExamResult(StarExamResultEnum.NOT_PASS.getCode());
        }
        return starExamResultVO;
    }
}
